﻿using System;
using System.Collections.Generic;
using MessageSystem.Managed.Infrastructure;
using MessageSystem.Managed.Interface;

namespace MessageSystem.Managed.Rpc
{
    public class RpcInnerMessage
    {
        public const int BroadcastMessageId = 0;
        public const int RpcRouteRegisterMessageId = 1;
    }

    public class RpcBroadcastMessage : IMessage
    {
        public int MessageId
        {
            get { return RpcInnerMessage.BroadcastMessageId; }
        }

        public int OpCode { get; set; }
        public ushort Flags { get; set; }
        public int Sessionid { get; set; }

        public List<RpcProxy> WriteProxies = null;
        public IMessage WriteMessage = null;

        public List<int> ReadSessionIds;
        public int ReadMessageId;

        public void Read(NetStream stream)
        {
            ReadSessionIds = new List<int>();
            int count = stream.ReadInt32();
            for (int i = 0; i < count; ++i)
            {
                ReadSessionIds.Add(stream.ReadInt32());
            }
            ReadMessageId = stream.ReadInt32();
        }

        public void Write(NetStream stream)
        {
            stream.WriteInt32(WriteProxies.Count);
            foreach (RpcProxy proxy in WriteProxies)
            {
                stream.WriteInt32(proxy.SessionId);
            }
            stream.WriteInt32(WriteMessage.MessageId);
            WriteMessage.Write(stream);
        }
    }

    class RpcRegisterMessage : IMessage
    {
        public int MessageId
        {
            get { return RpcInnerMessage.RpcRouteRegisterMessageId; }
        }

        public int OpCode { get; set; }
        public ushort Flags { get; set; }
        public int Sessionid { get; set; }

        public Int32 SessionId { get; set; }
        public Int32 ServiceId { get; set; }

        public void Read(NetStream stream)
        {
            SessionId = stream.ReadInt32();
            ServiceId = stream.ReadInt32();
        }

        public void Write(NetStream stream)
        {
            stream.WriteInt32(SessionId);
            stream.WriteInt32(ServiceId);
        }
    }
}
